<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
var jsonObj=new Object();
var hide_datagrid_column = false;

$(function(){
	
	showHide();
	lingDao_guanLi();
	
	
		   //给查询开始时间赋予今天日期初始值	
		   var curr_time = new Date();
		   var str = curr_time.getFullYear()+"-";
		   str += curr_time.getMonth()+1+"-";
		   str += curr_time.getDate();
		   $('#schedule_startime').datebox('setValue',str);
		   
		   
	hide_datagird_type_level();
	
	
	//弹出框设置
	$("#dialogDiv").dialog({
		title:"日程维护",//标题
		width: 700,//宽度
	    height: 410,//高度
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    onClose: function() {
			 $("#dialogIframe").attr("src","");
			} 
	});
	
	//查询按钮绑定点击事件
	$("#searchButton").click(function(){
		$('#carCategoryTable').datagrid({ queryParams:getParams()});
	});
	
	//新增按钮绑定点击事件
	$("#addButton").click(function(){
		$("#dialogIframe").attr("src","<c:url value='/schedule/schedule/scheduleAdd' />");
		$('#dialogDiv').dialog('open');
	});
	
	$("#addButton1").click(function(){
		$("#dialogIframe").attr("src","<c:url value='/schedule/schedule/scheduleAddLeadership' />");
		$('#dialogDiv').dialog('open');
	});
	
	//查看日程按钮点击事件
	$("#showButton").click(function(){
		var judge = '${judge}';
		if($("input[name='id']:checked").size()==1){
			$("#dialogIframe").attr("src","<c:url value='/schedule/show' />?judge="+judge+"&id="+$("input[name='id']:checked").val());
			$('#dialogDiv').dialog('open');
		}else{
			//"http://localhost:8080/document/pages/jsp/schedule/scheduleMaintenance.jsp?Judge="+judge+"&id=11"
			$.messager.alert('提示','请选择一条数据再查看');
		}
	});
	
	//编辑按钮绑定点击事件
	$("#updateButton").click(function(){
		//var judge = $("#Judge").val();
		var judge = '${judge}';
		if($("input[name='id']:checked").size()==1){
			$("#dialogIframe").attr("src","<c:url value='/schedule/scheduleEdit' />?judge="+judge+"&id="+$("input[name='id']:checked").val());
			$('#dialogDiv').dialog('open');
		}else{
			//"http://localhost:8080/document/pages/jsp/schedule/scheduleMaintenance.jsp?Judge="+judge+"&id=11"
			$.messager.alert('提示','请选择一条数据再编辑');
		}
	});
	
	//删除按钮绑定点击事件
	$("#deleteButton").click(function(){
		var ids="";
		var checkedObj=$("input[name='id']:checked");
		if(checkedObj.size()==1){
			ids=checkedObj.val();
		}else if(checkedObj.size()>1){
			for ( var i = 0; i < checkedObj.size(); i++) {
				ids+=(ids==""?"":",")+checkedObj[i].value;
			}
		}else{
			$.messager.alert('提示','请选择一条数据再删除');
			return;
		}
		if(jsonObj.sequence=="0"){
			$.messager.alert('提示','您所选择的数据已经是删除状态');
			return;
		}
		$.messager.confirm({
			title:"提示",
			msg:"确定删除吗？",
			top:100,
			fn:function(r){
				if(r){
					$.ajax({
						type: "POST",
						url: "<c:url value='/schedule/delete' />",
						data: "id="+ids,
						success: function(msg){
							if(msg.state=="200"){
								 $('#carCategoryTable').datagrid({ queryParams:getParams()});
							}else{
								$.messager.alert({
									title:"提示",
									msg:"数据删除失败！",
									top:100
								});
							}
						}
					});
				}
			}
		});
	});
	
	//物理删除
	$("#deleteButton_wuli").click(function(){
		var ids="";
		var checkedObj=$("input[name='id']:checked");
		if(checkedObj.size()==1){
			ids=checkedObj.val();
		}else if(checkedObj.size()>1){
			//$.messager.alert('提示','业务关系，请每次选择一条数据');
			for ( var i = 0; i < checkedObj.size(); i++) {
				ids+=(ids==""?"":",")+checkedObj[i].value;
			}
		}else{
			$.messager.alert('提示','请选择一条数据再删除');
			return;
		}
		if(jsonObj.sequence=="0"){
			$.messager.alert('提示','您所选择的数据已经是删除状态');
			return;
		}
		$.messager.confirm({
			title:'提示',
			msg:'确定删除？',
			top:100,
			fn:function(r){
				if(r){
					$.ajax({
						type: "POST",
						url: "<c:url value='/schedule/delete' />",
						data: "id="+ids,
						success: function(msg){
							if(msg.state=="200"){
								   /*$.messager.alert({
									   title:'提示',
										msg:'删除成功！',
										top:100
								   });*/
								   $('#carCategoryTable').datagrid({ queryParams:getParams()});
							   }else{
								   $.messager.alert({
										title:"提示",
										msg:"数据删除失败！",
										top:100
									});
							   }
						}
					});
				}
			}
		});
	});
	
	
	
	function writeScheduleData(data){
		var writeData = data.data; //转换为json数组
		$("#schedule_theme_Edit").val(writeData.theme);
		$("#schedule_type_Edit").val(writeData.type);
		$("#calendar_level_Edit").val(writeData.level);
		//$("#person_name_write").val(writeData.person);
		$('#schedule_startime_Edit').datetimebox('setValue', formatterSDate(new Date(writeData.startime)));
	    $('#schedule_endtime_Edit').datetimebox('setValue', formatterEDate(new Date(writeData.endtime)));
		$("#schedule_content_Edit").val(writeData.content);
	}
	
		
	//查询框初始显示	
	$("#schedulequeryListBtn").click();
	
	//表格加载
	$('#carCategoryTable').datagrid({
	    url:'<c:url value="/schedule/scheduleData"/>',
	    queryParams:getParams(),//提交参数
	    toolbar:'#carCategoryToolbar',//表头工具栏
	    pagination:true,//显示分页  
	    pagePosition:'bottom',//分页栏出现位置
	    pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    singleSelect:true,
	    columns:[[
			{field:'id',title:'',width:25,formatter: function(value,row,index){
				return '<input name="id" type="checkbox" value="'+value+'" />';
			}},
			{field:'person',title:'人员',align:'center'},
			{field:'type',title:'日程类型',align:'center',hidden:hide_datagrid_column,formatter: function(value,row,index){
				if(value=="1"){
					return "市政府主要领导召开会议";
				}else if(value=="2"){
					return "分管领导召开会议";
				}else if(value=="3"){
					return "公司专题会议";
				}else if(value=="4"){
					return "市级调研";
				}else if(value=="5"){
					return "公司级调研";
				}else if(value=="6"){
					return "市级会见座谈会";
				}else if(value=="7"){
					return "公司级会见座谈";
				}else{
					return"";
				}
			}},
			{field:'level',title:'优先级',align:'center',hidden:hide_datagrid_column,formatter:function(value,row,index){
				if(value=="1"){
					return "高";
				}else if(value=="2"){
					return "中";
				}else if(value=="3"){
					return "低";
				}else{
					return "";
				}
			}},
			{field:'startime',title:'开始时间',align:'center',formatter: function(value,row,index){
				var str="";
				if(value){
					str=dataFormatStr(new Date(value),"yyyy-MM-dd hh:mm:ss").replace(" ","<br/>");
				}
				return str;
			}},
			{field:'endtime',title:'结束时间',align:'center',formatter: function(value,row,index){
				var str="";
				if(value){
					str=dataFormatStr(new Date(value),"yyyy-MM-dd hh:mm:ss").replace(" ","<br/>");
				}
				return str;
			}},
			{field:'theme',title:'日程主题',align:'center'},
			{field:'content',title:'日程内容',align:'center'}
	    ]]
	});
});

//领导 和 管理权限 的权限按钮管理
function lingDao_guanLi(){
	var guanLi = "${boolScheduleAdmin}";
	var lingDao = "${lingDao}";
	var judge = ${judge};
	if(judge == 1 && guanLi == "0"){
		$("#add1").hide();
		$("#add2").hide();
		$("#edit").hide();
		$("#delete").hide();
		$("#delete_wuli").hide();
	}
	//if(guanLi == "0" && lingDao == "1"){
	//	$("#add1").hide();
	//	$("#add2").hide();
	//	$("#edit").hide();
	//	$("#delete").hide();
	//	$("#delete_wuli").hide();
	//}
}



//隐藏显示增加日程按钮
function showHide(){
	//var judge = $("#Judge").val();
	var judge = ${judge};
	if(judge == 2){
		$("#add1").show();
		$("#add2").hide();
		$("#person_").hide();
		$("#persons").hide();
		$("#deleteButton_wuli").show();
		$("#deleteButton").hide();
	}else{
		$("#add1").hide();
		$("#add2").show();
		$("#deleteButton_wuli").hide();
		$("#deleteButton").show();
	}
}








//查询条件生成方法
function getParams(){
	if($("#schedule_type").val()!="" && $("#schedule_type").val()!=null){
		jsonObj.type=$("#schedule_type").val();
	}else{
		jsonObj.type=null;
	}
	//$("#startTimeSearch").datetimebox("getValue");
	if($("#schedule_startime").datetimebox("getValue")!="" && $("#schedule_startime").datetimebox("getValue")!=null){
		jsonObj.startime = $("#schedule_startime").datebox("getValue")+" 00:00:00";
	}else{
		jsonObj.startime = null;
	}
	if($("#schedule_theme").val()!="" && $("#schedule_theme").val()!=null){
		jsonObj.theme=$("#schedule_theme").val();
	}else{
		jsonObj.theme=null;
	}
	if($("#schedule_endtime").datetimebox("getValue")!="" && $("#schedule_endtime").datetimebox("getValue") != null){
		jsonObj.endtime = $("#schedule_endtime").datebox("getValue")+" 23:59:59";
	}else{
		jsonObj.endtime = null;
	}
	if($("#schedule_person").val()!="" && $("#schedule_person").val()!=null){
		jsonObj.uuid=$("#schedule_person").val();
	}else{
		jsonObj.uuid=null;
	}
	//alert(list);
	//var judge = $("#Judge").val();
	var judge = ${judge};
	jsonObj.type_ =judge;
	return jsonObj;
}
//关闭弹出框
function closeDialog(){
	$('#carCategoryTable').datagrid({ queryParams:getParams()});
	$('#dialogDiv').dialog('close');
}

function hide_datagird_type_level(){
	var lingdao = "${lingDao}";
	var judge = ${judge};
	if(lingdao == "0" && judge == 2){
		hide_datagrid_column = true;
	}else{
		hide_datagrid_column = false;
	}
}
